<template>
  <div class="edit">
    <el-card class="box-card">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="宣传视频" prop="videoUrl">
          <el-radio-group v-model="uploadType" size="small">
            <el-radio :label="1">本地上传</el-radio>
            <el-radio :label="2">输入视频地址</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="视频封面图" v-if="uploadType === 1">
          <Upload v-model="form.videoCover" @clear="clearImageValidate('videoCover')" />
          <Upload type="video" v-model="form.videoUrl" @clear="clearImageValidate('videoUrl')" />
        </el-form-item>
        <el-form-item label="视频地址" v-if="uploadType === 2">
          <el-input v-model="form.videoUrl" placeholder="请输入视频地址" />
        </el-form-item>
        <el-form-item label="视频封面图地址" v-if="uploadType === 2">
          <el-input v-model="form.videoCover" placeholder="请输入视频封面图地址" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="cancel">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import Upload from "@/components/Upload";
import {
  getVideoBaseInfo,
  saveVideoBaseInfo
} from "@/api/system/webSiteConfig";

export default {
  name: "Video",
  components: {
    Upload
  },
  data() {
    return {
      // 表单参数
      form: {
        videoUrl: "",
        videoCover: ""
      },
      uploadType: 1,
      // 表单校验
      rules: {
        videoUrl: [
          {
            required: true,
            message: "请输入上传或输入宣传视频地址",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.getEditInfo();
  },
  methods: {
    clearImageValidate(field) {
      this.rules[field] = [];
      this.$refs["form"].clearValidate(field);
    },
    getEditInfo() {
      getVideoBaseInfo().then(response => {
        this.form = response.data ? response.data : {};
        this.uploadType = this.form.videoUrl ? 2 : 1;
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          saveVideoBaseInfo(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess("操作成功");
            }
          });
        }
      });
    },
    // 重置按钮
    cancel() {
      this.getEditInfo();
      this.resetForm("form");
    }
  },
  watch: {
    "form.videoUrl"(value) {
      if (value) {
        this.uploadType = 2;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.item {
  font-size: 14px;
  padding: 18px 0;
  span:first-child {
    width: 90px;
    display: inline-block;
    text-align: right;
  }
  span:last-child {
    margin-left: 20px;
    display: inline-block;
  }
  .image {
    width: 100px;
    height: 100px;
    margin-right: 20px;
  }
}
.deatil {
  display: flex;
  .avatar {
    width: 140px;
    height: 140px;
    margin-right: 20px;
    .img {
      height: 100%;
    }
    /deep/ img {
      border-radius: 50%;
    }
  }
  .info {
    flex: 1;
  }
}
</style>
